<?php

namespace App\Http\Controllers\Personal;
use App\Models\Personal\ResumeWorksImg;
use Illuminate\Http\Request;
use Style;

class ResumeWorksImgController extends CommonController{

    /**
     *选择上传图片文件
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function fileUpload(Request $request){
        $user = auth()->guard('personal')->user();
        if ($request->hasFile('file')) {
            $file=$request->file('file');
            $entension = strtolower($file -> getClientOriginalExtension());
            $clientName = $file -> getClientOriginalName();//客服端文件名
            //判断是不是一个有效文件
            if ($file->isValid()) {
                if (!in_array($entension, array('doc','docx','xls','xlsx','pdf','ppt','pptx','txt','gif','jpg','jpeg','bmp','png'))){
                    return response()->json(['error' => 304, 'info' => "文件类型出错，请重新上传文件！"]);
                }else{
                    $fileurl=date("Ym").'/';
                    $filename=time().strtolower(str_random(5)).'.'.$entension;
                    $request->file('file')->move(public_path().'/upload/personalworks/'.$fileurl,$filename);

                    $input = $request->only(['resume_id','works_id']);
                    $input['uid'] = $user->uid;
                    $input['addtime'] = date('Y-m-d H:i:s',time());
                    $input['updatetime'] = date('Y-m-d H:i:s',time());
                    $input['imgurl'] = $fileurl.$filename;
                    $input['originalimg'] = $clientName;//
                    ResumeWorksImg::create($input);

                    //判断文件是不是图片
                    if (in_array($entension, array('gif','jpg','jpeg','bmp','png'))){
                        $imgUrl = array(
                            'status' => 200,
                            'works_id'=>$request->input('works_id'),
                            'fileCon' =>asset('upload/personalworks/'.$fileurl.$filename),
                            'fileUrlImg' => asset('upload/personalworks/'.$fileurl.$filename)
                        );
                        return response()->json($imgUrl);
                    }else{
                        switch($entension){
                            case 'doc' : $imgurl = 'default/images/personal/doc.jpg'; break;
                            case 'docx' : $imgurl = 'default/images/personal/doc.jpg'; break;
                            case 'ppt' : $imgurl = 'default/images/personal/ppt.jpg'; break;
                            case 'pptx' : $imgurl = 'default/images/personal/ppt.jpg'; break;
                            case 'xls' : $imgurl = 'default/images/personal/xls.jpg'; break;
                            case 'xlsx' : $imgurl = 'default/images/personal/xls.jpg'; break;
                            case 'pdf' : $imgurl = 'default/images/personal/pdf.jpg'; break;
                            case 'txt' : $imgurl = 'default/images/personal/txt.jpg'; break;
                            default : $imgurl = '未知'; break;
                        }
                        $imgUrl = array(
                            'status' => 200,
                            'works_id'=>$request->input('works_id'),
                            'fileCon' =>asset('upload/personalworks/'.$fileurl.$filename),
                            'fileUrlImg' => asset($imgurl)
                        );
                        return response()->json($imgUrl);
                    }
                }
            }
        }else{
            return response()->json(['error' => 306, 'info' => "没有选择上传图片文件！"]);
        }
    }
}